package org.example.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.annotation.CheckPermission;
import org.example.pojo.domain.Artifact;
import org.example.pojo.domain.ArtifactMaintenance;
import org.example.pojo.vo.ArtMaintenancesVo;
import org.example.result.Result;
import org.example.service.ArtifactMaintenanceService;
import org.example.service.ArtifactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/artifact-maintenance")
public class ArtifactMaintenanceController {

    @Autowired
    private ArtifactMaintenanceService artifactMaintenanceService;
    @Autowired
    private ArtifactService artifactService;

    /**分页 获取某文物的所有维护记录(curator)**/
    @CheckPermission({"curator"})
    @GetMapping("/artifact/{artifactId}")
    public Result getMaintenancesByArtifactId(@PathVariable Integer artifactId,
                                              @RequestParam(defaultValue = "1") int page,
                                              @RequestParam(defaultValue = "10") int size) {
        PageHelper.startPage(page,size);
        List<ArtifactMaintenance> maintenances = artifactMaintenanceService.getMaintenancesByArtifactId(artifactId);

        return Result.success("文物("+artifactId+")的所有维护记录分页",new PageInfo<>(maintenances));
    }

    /**根据维护ID查询维护记录(curator)**/
    @CheckPermission({"curator"})
    @GetMapping("/{maintenanceId}")
    public Result getMaintenanceById(@PathVariable Integer maintenanceId) {
        ArtifactMaintenance maintenance = artifactMaintenanceService.getMaintenanceById(maintenanceId);
        return Result.success("维护ID("+maintenanceId+")查询维护记录",maintenance);
    }

    /**添加新维护记录(curator)**/
    @CheckPermission({"curator"})
    @PostMapping("/add")
    public Result addMaintenance(@RequestBody ArtifactMaintenance maintenance) {
        artifactMaintenanceService.addMaintenance(maintenance);
        return Result.success("维护记录添加成功！");
    }

    /**更新维护记录(curator)**/
    @CheckPermission({"curator"})
    @PutMapping("/update")
    public Result updateMaintenance(@RequestBody ArtifactMaintenance maintenance) {
        artifactMaintenanceService.updateMaintenance(maintenance);
        return Result.success("维护记录更新成功！");
    }

    /**删除维护记录(curator)**/
    @CheckPermission({"curator"})
    @DeleteMapping("/delete/{maintenanceId}")
    public Result deleteMaintenance(@PathVariable Integer maintenanceId) {
        artifactMaintenanceService.deleteMaintenance(maintenanceId);
        return Result.success("维护记录删除成功！");
    }
}
